$(document).ready(function(){
	  $("input[name='empName']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empNameNULL").html("名字不能为空!");
				 $(".empNameNULL").show();
			}else {
				 $(".empNameNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empEnglishName']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empEnglishNameNULL").html("英文名称不能为空!");
				 $(".empEnglishNameNULL").show();
			}else {
				 $(".empEnglishNameNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empPinYinName']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empPinYinNameNULL").html("拼音不能为空!");
				 $(".empPinYinNameNULL").show();
			}else {
				 $(".empPinYinNameNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empeMail']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empeMailNULL").html("邮箱不能为空!");
				 $(".empeMailNULL").show();
			}else {
				 $(".empeMailNULL").hide();	
				 $.ajax({
			         type:"get",
			         url: "${pageContext.request.contextPath}/position/check/email/"+str,
			         dataType : "json",
			         success:function(datas){
			       	 	if (512 == datas.state) {
			       	 		$(".empeMailNULL").html("email已被注册!");
			       	 	 	$(".empeMailNULL").show();
						}   	 
			         }
			     });	
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empMobile']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empMobileNULL").html("手机不能为空!");
				 $(".empMobileNULL").show();
			}else {
				 $(".empMobileNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empTel']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empTelNULL").html("座机不能为空!");
				 $(".empTelNULL").show();
			}else {
				 $(".empTelNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empPositionId']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empPositionIdNULL").html("职位不能为空!");
				 $(".empPositionIdNULL").show();
			}else {
				 $(".empPositionIdNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empDepartmentId']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empDepartmentIdNULL").html("部门不能为空!");
				 $(".empDepartmentIdNULL").show();
			}else {
				 $(".empDepartmentIdNULL").hide();		 
		}
	  });
	});
$(document).ready(function(){
	  $("input[name='empStatus']").blur(function(){
		  var str =  $.trim($(this).val());
			if ("" == str) {
				 $(".empStatusNULL").html("职员状态不能为空!");
				 $(".empStatusNULL").show();
			}else {
				 $(".empStatusNULL").hide();		 
		}
	  });
	});
function getdept() {
	 $.ajax({
       type:"get",
       url: "${pageContext.request.contextPath}/dept/dept",
       dataType : "json",
       success:function(datas){
     	  $("select[name='empDepartmentId']").html('<option value="">---请选择---</option>');
     	  $.each(datas.data, function(index, dept) {
     	     $("select[name='empDepartmentId']").append('<option value="'+dept.departmentId+'">'+dept.departmentName+'</option>');          		  
     	  });
       }
   });
}
function getposition() {
	 $.ajax({
       type:"get",
       url: "${pageContext.request.contextPath}/position/position",
       dataType : "json",
       success:function(datas){
     	  $("select[name='empPositionId']").html('<option value="">---请选择---</option>');
     	  $.each(datas.data, function(index, position) {
     	     $("select[name='empPositionId']").append('<option value="'+position.positionId+'">'+position.positionName+'</option>');          		  
     	  });
       }
   });
}
function getstauts() {
	 $.ajax({
       type:"get",
       url: "${pageContext.request.contextPath}/es/es",
       dataType : "json",
       success:function(datas){
     	  $("select[name='empStatus']").html('<option value="">---请选择---</option>');
     	  $.each(datas.data, function(index, employeeStatus) {
     	     $("select[name='empStatus']").append('<option value="'+employeeStatus.employeeStatusId+'">'+employeeStatus.employeeStatusName+'</option>');          		  
     	  });
       }
   });
}
function initTable() {
$.ajax({
  url : "${pageContext.request.contextPath}/employee/employee",
  type : "Get",
  dataType : "json",
 // contentType : "application/json; charset=utf-8",
  success : function(datas) {
      var departmentVal = [];
      $.each(datas.data.employees, function(index, d) {
      	departmentVal.push(d);      
      });
  $('#empTable').bootstrapTable(
        {
             data : departmentVal, //最终的JSON数据放在这里                    
             toolbar : '#toolbar',
             pagination : true,
             pageNumber : 1,
             pageSize : 10,
             pageList : [5, 10, 15, 20, 25],
             search : true,                 
             showColumns : true,
             uniqueId: "empId",  
             height: 500,       
             showRefresh: true,  
             showExport : true, //是否显示导出
             exportDataType : "selected", //basic', 'all', 'selected'.
             sidePagination : "server",
             clickToSelect : true,
             showToggle : true, //是否显示详细视图和列表视图的切换按钮
             cardView : false, //是否显示详细视图
             detailView : false,
             
         /*     queryParamsType : "undefined",
             queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    pageNumber: params.pageNumber,
                    pageSize: params.pageSize,
                    orderNum : $("#txt_search_departmentname").val(),
                    page:$("#txt_search_statu").val()
                };
                return param;
             } , */
             
             minimunCountColumns : 2,
             columns: [{
                 checkbox: true
             }, {
                 field: 'empId',
                 title: '职员编号'
             }, {
                 field: 'empName',
                 title: '职员姓名'
             }, {
                 field: 'empPinYinName',
                 title: '职员拼音'
             }, {
                 field: 'empEnglishName',
                 title: '职员英文名'
             }, {
                 field: 'empeMail',
                 title: '职员邮箱'
             }, {
                 field: 'empMobile',
                 title: '职员手机'
             }, {
                 field: 'empTel',
                 title: '职员座机'
             }, {
                 field: 'empAddress',
                 title: '职员地址'
             }, {
                 field: 'empGender',
                 title: '职员性别'
             }, {
                 field: 'empPositionId',
                 title: '职员职位'
             },{
                 field: 'empDepartmentId',
                 title: '职员部门'
             },{
                 field: 'empStatus',
                 title: '职员状态'
             }, {
                 field: 'empStartTime',
                 title: '入职日期'
             }, {
                 field: 'empCreateTime',
                 title: '创建时间'
             }, {
                 field: 'empCreateName',
                 title: '创建人名'
             }, {
                 field: 'empDecription',
                 title: '职员描述'
             }, ]
              });
  	}
});

}

$(document).ready(function () {
  //调用函数，初始化表格
  initTable();

  //当点击查询按钮的时候执行
  $("#btn_query").bind("click", initTable);
  
//删除：
  $('#btn_delete').on("click", function () {
	  getstauts();
  	var row=$.map($("#empTable").bootstrapTable('getSelections'),function(row){
          return row ;
      });
  	if(row.length==1){
          for (var i = 0; i < row.length; i++) {
              alert(row[i].empId);
              //打开弹出框：
              $('#myDeleteModal').modal();
              $('#empId').val(row[i].empId);
              $('#deleteemp').val(row[i].empStatus);
          }
      }
  });

  //添加：
  $('#btn_add').on("click", function () {
  	getdept();
  	getposition();
  	getstauts();
      $('#myModal').modal();
  });

  //修改
  $('#btn_edit').on("click", function () {

  	getdept();
  	getposition();
      var row=$.map($("#empTable").bootstrapTable('getSelections'),function(row){
          return row ;
      });
      if(row.length==1){
          for (var i = 0; i < row.length; i++) {
              alert(row[i].pid);
              //打开弹出框：
              $('#updatemyModal').modal();
              $('#updateempId').val(row[i].empId);
              $('#updateempName').val(row[i].empName);
              $('#updateempEnglishName').val(row[i].empEnglishName);
              $('#updateempPinYinName').val(row[i].empPinYinName);
              $('#updateempeMail').val(row[i].empeMail);
              $('#updateempGender').val(row[i].empGender);
              $('#updateempMobile').val(row[i].empMobile);
              $('#updateempTel').val(row[i].empTel);
              $('#updateempAddress').val(row[i].empAddress);
              $('#updateempPhoto').val(row[i].empPhoto);
              $('#updateempPositionId').val(row[i].empPositionId);
              $('#updateempDepartmentId').val(row[i].empDepartmentId);
              $('#updateempStatus').val(row[i].empStatus);
              $('#updateempStartTime').val(row[i].empStartTime);
              $('#updateempDecription').val(row[i].empDecription);
          }
      }
  });


})
//提交删除
  function mydelete(){
  	var empId =  $('#empId').val();
  	var empStatus = $('#deleteemp').val();
  	alert(empStatus);
      $.ajax({
          type:"DELETE" ,
          url:"${pageContext.request.contextPath }/employee/employee/"+empId+"/"+empStatus,
          contentType : "application/json; charset=utf-8",
          success:function(datas){
              if(203==datas.state){
                  $('#empTable').bootstrapTable('refresh');
              }else{
                  alert('删除失败');
              }

          }
      });
  }

//提交添加：
function mya(){
	var empName = $('#addempName').val();
	var empEnglishName =  $('#addempEnglishName').val();
	var empPinYinName =  $('#addempPinYinName').val();
	var empeMail =  $('#addempeMail').val();
	var empGender =  $('#addempGender').val();
	var empMobile =  $('#addempMobile').val();
	var empTel =  $('#addempTel').val();
	var empAddress =  $('#addempAddress').val();
	var empPhoto =  $('#addempPhoto').val();
	var empPositionId =  $('#addempPositionId').val();
	var empDepartmentId =  $('#addempDepartmentId').val();
	var empStatus =  $('#addempStatus').val();
	var empStartTime =  $('#addempStartTime').val();
	var empDecription =  $('#addempDecription').val();
  //实例化一个对象：
  /* var person={"pname":addpname,"page":addpage}; */
  var employee = {"empName":empName,"empEnglishName":empEnglishName,"empPinYinName":empPinYinName,"empeMail":empeMail,
  		"empGender":empGender,"empMobile":empMobile,"empTel":empTel,"empAddress":empAddress,
  		"empPhoto":empPhoto,"empPositionId":empPositionId,"empDepartmentId":empDepartmentId,
  		"empStatus":empStatus,"empStartTime":empStartTime,
  		"empDecription":empDecription};
  //将对象转换成字符串
  /* var str=JSON.stringify(employee); */
  /* alert(str); */
  $.ajax({
      type:"POST" ,
      url:"${pageContext.request.contextPath }/employee/employee/add",
      contentType : "application/json; charset=utf-8",
      data:JSON.stringify(employee),
      success:function(datas){
      	console.log(datas)
          if(datas.state==202){
              $('#empTable').bootstrapTable('refresh');
              //$('#ff_p').form('clear');
          }else{
              alert('添加失败');
          }

      }
  });
}

//修改提交：
function myb(){
	var empId =  $('#updateempId').val();
	var empName = $('#updateempName').val();
	var empEnglishName =  $('#updateempEnglishName').val();
	var empPinYinName =  $('#updateempPinYinName').val();
	var empeMail =  $('#updateempeMail').val();
	var empGender =  $('#updateempGender').val();
	var empMobile =  $('#updateempMobile').val();
	var empTel =  $('#updateempTel').val();
	var empAddress =  $('#updateempAddress').val();
	var empPhoto =  $('#updateempPhoto').val();
	var empPositionId =  $('#updateempPositionId').val();
	var empDepartmentId =  $('#updateempDepartmentId').val();
	var empStatus =  $('#updateempStatus').val();
	var empStartTime =  $('#updateempStartTime').val();
	var empDecription =  $('#updateempDecription').val();
  //实例化一个对象：
  /* var person={"pname":addpname,"page":addpage}; */
  var employee = {"empId":empId,"empName":empName,"empEnglishName":empEnglishName,"empPinYinName":empPinYinName,"empeMail":empeMail,
  		"empGender":empGender,"empMobile":empMobile,"empTel":empTel,"empAddress":empAddress,
  		"empPhoto":empPhoto,"empPositionId":empPositionId,"empDepartmentId":empDepartmentId,
  		"empStatus":empStatus,"empStartTime":empStartTime,
  		"empDecription":empDecription};
  //将对象转换成字符串
  /* var str=JSON.stringify(employee); */
  $.ajax({
      type:"PUT" ,
      url:"${pageContext.request.contextPath }/employee/employee/update",
      contentType : "application/json; charset=utf-8",
      data:JSON.stringify(employee),
      success:function(datas){
          if(205==datas.state){
              $('#empTable').bootstrapTable('refresh');
          }else{
              alert('修改失败');
          }

      }
  });
}